package com.hjwoo.app.views;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hjwoo.app.R;
import com.hjwoo.app.adpter.LeadsLVAdapter;
import com.hjwoo.app.baseviews.BaseAppCompatActivity;
import com.hjwoo.app.been.FailureResultObject;
import com.hjwoo.app.been.OrdersObject;
import com.hjwoo.app.fragment.UserCenterFragment;
import com.hjwoo.app.net.RequestListener;
import com.hjwoo.app.net.RequestMethod;
import com.hjwoo.app.net.RequestTag;
import com.hjwoo.app.utils.JsonParser;
import com.hjwoo.app.utils.LoanAccessTokenIdKeeper;
import com.hjwoo.app.utils.LoanLog;
import com.hjwoo.app.utils.LoanToast;
import com.hjwoo.app.utils.Utils;

import java.util.LinkedList;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import in.srain.cube.views.ptr.header.StoreHouseHeader;

public class OrdersActivity extends BaseAppCompatActivity implements RequestListener
{
      private Gson gson = new Gson();
    private String[]                 stages;
    private String[]                 status;
    private String                   itemName;
    private ListView                 orders_lv;
    private LinkedList<OrdersObject> orders;
    private LeadsLVAdapter leadsLVAdapter;
    //    有没有调用onresume方法
    private boolean onRestartFlag = false;
    private PtrClassicFrameLayout mPtrFrame;
    private LinearLayout neterror;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_orders);
        if (getIntent() != null)
        {
            stages = getIntent().getStringArrayExtra("stages");
            status = getIntent().getStringArrayExtra("status");
            itemName = getIntent().getStringExtra(UserCenterFragment.ITEMNAME);
            if (!itemName.equals("已评房"))
            {
                findViewById(R.id.orders_pingfang_btn).setVisibility(View.GONE);
                TextView child_orders_show_tv = (TextView) findViewById(R.id.child_orders_show_tv);
                child_orders_show_tv.setText("您还没有此类订单！");

            }
        }
        initView();
    }

    void initView()
    {
        Utils.initTitle(itemName, this, true);
         orders_lv = (ListView) findViewById(R.id.orders_lv);
        neterror = (LinearLayout) findViewById(R.id.neterror_linear);
        neterror.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                RequestMethod.getMethod().queryByStage(stages, status, OrdersActivity.this, OrdersActivity.this);
            }
        });
        mPtrFrame = (PtrClassicFrameLayout) findViewById(R.id.orders_header_list_view_frame);
// header custom begin
        final StoreHouseHeader header = new StoreHouseHeader(OrdersActivity.this);
        header.setPadding(0, Utils.dp2px(OrdersActivity.this, 15), 0, 0);
        header.initWithString("HJWoo");
        header.setTextColor(getResources().getColor(R.color.gray));
        mPtrFrame.setHeaderView(header);
        mPtrFrame.addPtrUIHandler(header);
        // header custom end

        // 下拉刷新
        mPtrFrame.setLastUpdateTimeRelateObject(this);
        mPtrFrame.setPtrHandler(new PtrHandler()
        {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame)
            {
                //        获取订单列表
                RequestMethod.getMethod().queryByStage(stages, status, OrdersActivity.this, OrdersActivity.this);

            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header)
            {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
            }
        });

        orders_lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
            {
                Intent mIntent = new Intent(OrdersActivity.this, OrderTrackingActivity.class);
                Bundle mBundle = new Bundle();
                mBundle.putSerializable("order", orders.get(i));
                mIntent.putExtras(mBundle);
                startActivity(mIntent);
            }
        });

        findViewById(R.id.orders_pingfang_btn).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                if (!LoanAccessTokenIdKeeper.readAccessToken(OrdersActivity.this).getAppSessionId().equals(""))
                {
                    startActivity(new Intent(OrdersActivity.this, CommunityActivity.class));
                }
                else
                {
                    //请登录
                    MainActivity.currIndex = 3;
                    MainActivity.showFragment();
                    MainActivity.group.check(R.id.main_footbar_user);
                    LoanToast.showToastShort("请登录", OrdersActivity.this);

                }
            }
        });

    }


    @Override
    public void requestSuccess(String tag, String message)
    {
        mPtrFrame.refreshComplete();
        mPtrFrame.setVisibility(View.VISIBLE);
        neterror.setVisibility(View.GONE);
        if (tag.equals(RequestTag.QUERYBYSTAGE))
        {
            orders = JsonParser.getJsonParser().parserJosnToList(message);
            if (orders.size() == 0)
            {
                findViewById(R.id.orders_isnull_rl).setVisibility(View.VISIBLE);
                neterror.setVisibility(View.GONE);
            }
            else
            {
                findViewById(R.id.orders_isnull_rl).setVisibility(View.GONE);
                leadsLVAdapter = new LeadsLVAdapter(OrdersActivity.this, orders);
                orders_lv.setAdapter(leadsLVAdapter);
            }
        }

    }

    @Override
    public void requestError(String message)
    {
//        neterror.setVisibility(View.VISIBLE);
        findViewById(R.id.orders_isnull_rl).setVisibility(View.GONE);
        mPtrFrame.refreshComplete();
        FailureResultObject failureResultObject = new FailureResultObject();
        failureResultObject = gson.fromJson(message, FailureResultObject.class);
        LoanToast.showToastShort(failureResultObject.getErrorMessage(), OrdersActivity.this);
    }

    @Override
    protected void onRestart()
    {
        super.onRestart();
        RequestMethod.getMethod().queryByStage(stages, status, OrdersActivity.this, this);
        onRestartFlag = true;
        LoanLog.e("ord", "onr");
    }

    @Override
    protected void onStop()
    {
        super.onStop();
        LoanLog.e("ord", "onStop");
    }

    @Override
    protected void onResume()
    {
        super.onResume();
//        获取订单列表
        RequestMethod.getMethod().queryByStage(stages, status, OrdersActivity.this, this);
    }
}